import 'package:jpush_flutter/jpush_flutter.dart';

// 定义一个回调类型，用于在很多方法中返回结果
typedef JPushCallback = void Function(String? result);

// 一个用于管理 JPush 相关操作的工具类，如推送通知、标签和别名。
class JPushTool {
  // JPush 实例，用于推送通知功能
  final JPush _jpush = JPush();

  // 构造函数，初始化 JPush
  JPushTool();

  // 私有方法，初始化 JPush 设置和事件处理器
  Future<void> initializeJPush() async {
    // 设置 JPush 的认证信息
    _jpush.setAuth(enable: true);
    // // 配置 JPush 的 appKey、渠道、环境（生产或调试）
    _jpush.setup(
      appKey: "18b195a6ecd49ba223b7e2c7", // 请替换为你的 appKey
      channel: "developer-default",
      production: false, // 生产环境设置为 true
      debug: true, // 开启调试模式，方便开发
    );
    // 应用推送通知的权限（iOS 特有设置）
    _jpush.applyPushAuthority(
      const NotificationSettingsIOS(sound: true, alert: true, badge: true),
    );

    // 设置事件处理器，处理不同的推送通知事件
    _jpush.addEventHandler(
      // 收到通知时调用
      onReceiveNotification: (Map<String, dynamic> message) async {
        print("JPush onReceiveNotification: $message");
      },
      // 打开通知时调用
      onOpenNotification: (Map<String, dynamic> message) async {
        print("JPush onOpenNotification: $message");
      },
      // 收到自定义消息时调用
      onReceiveMessage: (Map<String, dynamic> message) async {
        print("JPush onReceiveMessage: $message");
      },
      // 设备连接到推送服务时调用
      onConnected: (Map<String, dynamic> message) async {
        print("JPush onConnected: $message");
      },
    );
  }

  // 获取设备的注册 ID，回调返回 ID 或错误信息
  Future<void> getRegistrationID(JPushCallback callback) async {
    try {
      String? registrationID = await _jpush.getRegistrationID();
      callback(registrationID);
    } catch (error) {
      callback("Error: $error");
    }
  }

  // 发送本地通知
  Future<void> sendLocalNotification(LocalNotification notification) async {
    try {
      String? result = await _jpush.sendLocalNotification(notification);
      print("JPush sendLocalNotification: $result");
    } catch (error) {
      print("Error sending local notification: $error");
    }
  }

  // 设置设备的标签
  Future<void> setTags(List<String> tags, JPushCallback callback) async {
    try {
      Map map = await _jpush.setTags(tags);
      callback("Tags set successfully: ${map['tags']}");
    } catch (error) {
      callback("Error setting tags: $error");
    }
  }

  // 给设备添加标签
  Future<void> addTags(List<String> tags, JPushCallback callback) async {
    try {
      Map map = await _jpush.addTags(tags);
      callback("Tags added successfully: ${map['tags']}");
    } catch (error) {
      callback("Error adding tags: $error");
    }
  }

  // 删除设备的标签
  Future<void> deleteTags(List<String> tags, JPushCallback callback) async {
    try {
      Map map = await _jpush.deleteTags(tags);
      callback("Tags deleted successfully: ${map['tags']}");
    } catch (error) {
      callback("Error deleting tags: $error");
    }
  }

  // 获取设备的所有标签
  Future<void> getAllTags(JPushCallback callback) async {
    try {
      Map map = await _jpush.getAllTags();
      callback("All tags: ${map['tags']}");
    } catch (error) {
      callback("Error retrieving tags: $error");
    }
  }

  // 清除设备的所有标签
  Future<void> cleanTags(JPushCallback callback) async {
    try {
      Map map = await _jpush.cleanTags();
      callback("Tags cleaned: ${map['tags']}");
    } catch (error) {
      callback("Error cleaning tags: $error");
    }
  }

  // 设置设备的别名
  Future<void> setAlias(String alias, JPushCallback callback) async {
    try {
      Map map = await _jpush.setAlias(alias);
      callback("Alias set successfully: $map");
    } catch (error) {
      callback("Error setting alias: $error");
    }
  }

  // 删除设备的别名
  Future<void> deleteAlias(JPushCallback callback) async {
    try {
      Map map = await _jpush.deleteAlias();
      callback("Alias deleted successfully: $map");
    } catch (error) {
      callback("Error deleting alias: $error");
    }
  }

  // 获取设备的别名
  Future<void> getAlias(JPushCallback callback) async {
    try {
      Map map = await _jpush.getAlias();
      callback("Alias retrieved: $map");
    } catch (error) {
      callback("Error retrieving alias: $error");
    }
  }

  // 停止推送通知
  void stopPush() {
    _jpush.stopPush();
    print("JPush stopped");
  }

  // 恢复推送通知
  void resumePush() {
    _jpush.resumePush();
    print("JPush resumed");
  }

  // 清除所有通知
  void clearAllNotifications() {
    _jpush.clearAllNotifications();
    print("All notifications cleared");
  }

  // 设置应用的图标徽章数字（iOS 使用）
  Future<void> setBadge(int badgeNumber, JPushCallback callback) async {
    try {
      Map map = await _jpush.setBadge(badgeNumber);
      callback("Badge set to $badgeNumber: $map");
    } catch (error) {
      callback("Error setting badge: $error");
    }
  }

  // 检查通知是否已启用
  Future<void> isNotificationEnabled(JPushCallback callback) async {
    try {
      bool isEnabled = await _jpush.isNotificationEnabled();
      callback("Notification enabled: $isEnabled");
    } catch (error) {
      callback("Error checking notification status: $error");
    }
  }

  // 打开推送通知设置页面（iOS）
  void openSettingsForNotification() {
    _jpush.openSettingsForNotification();
    print("Opened notification settings");
  }

  // 记录页面进入事件（用于页面性能跟踪）
  void pageEnterTo(String pageName) {
    _jpush.pageEnterTo(pageName);
    print("Entered page: $pageName");
  }

  // 记录页面离开事件（用于页面性能跟踪）
  void pageLeave(String pageName) {
    _jpush.pageLeave(pageName);
    print("Left page: $pageName");
  }
}
